<?php
require_once(dirname(__FILE__) . '/base.class.php');
class User extends Base
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }


    /*
        获取会员列表
    */
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        }
        if (isset($_GET['page_num'])) {
            $pageNum = $_GET['page_num'];
        }
        if (!isset($page)) {
            $page = 1;
        }
        if (!isset($pageNum)) {
            $pageNum = 12;
        }
        $where = '1=1';
        if (isset($_GET['nickname'])) {
            $nickname = $_GET['nickname'];
            $where .= " and nickname like '%{$nickname}%'";
        }
        if (isset($_GET['username'])) {
            $username = $_GET['username'];
            $where .= " and username like '%{$username}%'";
        }
        if (isset($_GET['tel'])) {
            $tel = $_GET['tel'];
            $where .= " and tel like '%{$tel}%'";
        }
        if (isset($_GET['email'])) {
            $email = $_GET['email'];
            $where .= " and email like '%{$email}%'";
        }
        $offset = ($page - 1) * $pageNum;
        $list = $this->db->query("select * from users where {$where} limit {$offset},{$pageNum}");
        $total = $this->db->get_one("select count(id) as count from users where {$where}")['count'];
        // 将头像路径处理成绝对路径
        foreach($list as $key=>$item){
            $list[$key]['avatar']=path($item['avatar']);
        }
        $output['data']['list'] = $list;
        $output['data']['total'] = $total;
        // $output['sql']="select * from users where {$where} limit {$offset},{$pageNum}";
        echo json_encode($output);
    }

    /*
        添加会员账号
    */
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (!empty($_POST)) {
            $data = $_POST;
            $unique = $this->check_user_unique($data['username']);
            if (!$unique) {
                $output['errcode'] = 1;
                $output['msg'] = '用户名不可用';
            } else {
                $username = $data['username'];
                $password = md5($data['password']);
                $sql = "insert into users set username='{$username}',password='{$password}'";
                $res = $this->db->query($sql);
                if ($res) {
                    $output['msg'] = '账号注册成功';
                } else {
                    $output['msg'] = '账号注册失败';
                }
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用post方式提交';
        }
        echo json_encode($output);
    }

    /*
        密码重置: 管理后台重置用户密码
    */
    function reset_pwd()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $user_id = $_GET['id'];
            if ($user_id > 0) {
                $password = md5($_POST['password']);
                $sql = "update users set password='{$password}' where id={$user_id}";
                $res = $this->db->query($sql);
                if (!$res) {
                    $output['errcode'] = 3;
                    $output['msg'] = '重置密码失败';
                }
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '参数错误';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交';
        }
        echo json_encode($output);
    }


    /*
        获取会员信息
    */
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "select * from users where id={$id} limit 1";
            $data = $this->db->get_one($sql);
            // 将头像路径处理成绝对路径
            $data['avatar'] = path($data['avatar']);
            $output['data'] = $data;
        } else {
            $output['errcode'] = '1';
            $output['msg'] = '参数错误';
        }
        echo  json_encode($output);
    }

    /*
        更新用户信息
    */
    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $user_id = $_GET['id'];
            if ($user_id > 0) {
                $query_str = arr2str($_POST);
                $sql = "update users set {$query_str} where id={$user_id}";
                $res = $this->db->query($sql);
                if (!$res) {
                    $output['errcode'] = 4;
                    $output['msg'] = '信息更新失败';
                }
            } else {
                $output['errcode'] = 3;
                $output['msg'] = '参数错误';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '请使用POST方式提交';
        }
        echo json_encode($output);
    }



    // 校验用户名是否唯一
    private function check_user_unique($username)
    {
        $sql = "select id,username from users where username='{$username}'";
        $res = $this->db->get_one($sql);
        if (empty($res)) {
            // 用户名可用
            return true;
        } else {
            // 用户名不可用
            return false;
        }
    }
}
